import 'package:flutter/material.dart';


void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter 导航系统',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),

      debugShowCheckedModeBanner: false,

      // 命名路由配置
      routes: {
    '/':(context)=>const HomePage(),
    '/detail':(context){
    // 从路由参数中获取数据
    final args = ModalRoute.of(context)!.settings.arguments as Map<String,dynamic>?;
    return DetailPage(
    productId:args?['id']??0,
    productName:args?['name']??'未知商品');
    },
    },
      // 高级路由生成（处理未定义路由）
      onGenerateRoute: (settings) {
        if (settings.name == '/settings') {
          return MaterialPageRoute(
              builder: (context) => const SettingsPage(),
              settings: settings);
        }
        return null;
      },

    );
  }
}

///首页
class HomePage extends StatelessWidget {
  const HomePage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('首页')),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            // 1. 基础导航跳转
            ElevatedButton(onPressed: () {
              Navigator.push(context,
                MaterialPageRoute(
                    builder: (context) => const BasicPage(),
                ),
              );
            },
                child:
                const Text('基础导航跳转')
            ),

            const SizedBox(height: 20),
            // 2. 构造函数传参
            ElevatedButton(onPressed: () {
              Navigator.push(context,
                MaterialPageRoute(builder: (context) =>
                    DetailPage(
                        productId: 1001, productName: '高端智能手机'),),);
            },
              child: const Text('构造函数传参'),
            ),

            const SizedBox(height: 20),
            // 3. 命名路由传参
            ElevatedButton(onPressed: () {
              Navigator.pushNamed(context, '/detail',
                  arguments: {
                    'id': 2002,
                    'name': '智能手表'
                  });
            }, child: const Text('命名路由传参'),),

            const SizedBox(height: 20),
            //4. 带返回结果的导航
            ElevatedButton(onPressed: () async {
              final result = await Navigator.push(context,
                  MaterialPageRoute(builder: (context) => const PaymentPage(),
                  ));
              if(result!=null && context.mounted){//空安全处理
                ScaffoldMessenger.of(context).showSnackBar(
                  SnackBar(content: Text('支付结果：${result['status']}'))
                );
              }
            },              
                child: const Text('带返回结果的导航')),
            const SizedBox(height: 20),
            // 5. 动态路由生成
            ElevatedButton(onPressed: () {
              Navigator.pushNamed(context, '/settings');
            }, child: const Text('动态路由生成'))
          ],
        ),
      ),
    );
  }
}

/// 基础页面（无参数）
class BasicPage extends StatelessWidget {
  const BasicPage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text("基础页面")),
      body: Center(
        child: ElevatedButton(
          onPressed: () => Navigator.pop(context),
          child: const Text('返回首页'),
        ),
      ),
    );
  }
}

class DetailPage extends StatelessWidget {
  final int productId;
  final String productName;

  const DetailPage({
    super.key,
    required this.productId,
    required this.productName,
  });

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('$productName 详情'),),
      body: Padding(
        padding:  const EdgeInsets.all(20),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text('商品ID: $productId',style: const TextStyle(fontSize: 24),),
            Text('商品名称: $productName',style: const TextStyle(fontSize: 20),),
            const Spacer(),
            Center(
              child: ElevatedButton(
                onPressed: ()=>Navigator.pop(context),
                child: const Text('返回'),
              ),
            )
          ],
        ),
      ),
    );
  }


}

class PaymentPage extends StatelessWidget{
  const PaymentPage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar:AppBar(title: const Text('支付页面'),),
        body:Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              const Text('请完成支付操作',style: TextStyle(fontSize: 24)),
              const SizedBox(height: 40),
              Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  ElevatedButton(
                      onPressed: ()=>Navigator.pop(context,{'status':'支付成功'}),
                      style: ElevatedButton.styleFrom(backgroundColor: Colors.green),
                      child: const Text('支付成功')),
                  const SizedBox(width: 20),
                  ElevatedButton(
                      onPressed: ()=>Navigator.pop(context,{'status':'支付失败'}),
                      style: ElevatedButton.styleFrom(backgroundColor: Colors.red),
                      child: const Text('支付失败'))
                ],
              )
            ],
          ),
        )
    );
  }


}

class SettingsPage extends StatelessWidget{
  const SettingsPage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('设置页面'),),
      body: Center(
        child: ElevatedButton(
          onPressed: ()=>Navigator.pop(context),
          child: const Text('返回首页'),
        ),
      ),
    );
  }

}





